package com.person.jiaiplan.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.person.jiaiplan.assembler.AccountMemberAssembler;
import com.person.jiaiplan.pojo.R;
import com.person.jiaiplan.pojo.dto.expenses.AccountMemberAddReqDTO;
import com.person.jiaiplan.pojo.dto.expenses.AccountMemberInfoResDTO;
import com.person.jiaiplan.pojo.dto.expenses.AccountMemberQueryReqDTO;
import com.person.jiaiplan.pojo.entity.AccountMemberInfo;
import com.person.jiaiplan.service.AccountMemberInfoService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员账号接口
 */
@AllArgsConstructor
@RestController
@RequestMapping("member")
@Validated
public class AccountMemberController {

    @Autowired
    private AccountMemberInfoService accountMemberInfoService;

    /**
     * 添加账号
     *
     * @param
     * @return
     */
    @PostMapping("add")
    public R<Boolean> addAccountInfo(@RequestBody AccountMemberAddReqDTO reqDTO) {
        return R.ok(accountMemberInfoService.addAccountInfo(reqDTO));
    }

    @GetMapping("queryOne")
    public R<AccountMemberInfoResDTO> queryOne(@Param("countId") String countId){
        AccountMemberInfo accountMemberInfo = accountMemberInfoService.getOne(Wrappers.<AccountMemberInfo>lambdaQuery().eq(AccountMemberInfo::getCountId, Long.valueOf(countId)));
        if (accountMemberInfo == null){
            return R.ok(null);
        }else{
            return R.ok(AccountMemberAssembler.INSTANCE.accountMemberinfoToResDTO(accountMemberInfo));
        }

    }

    @PostMapping("queryList")
    public R<List<AccountMemberInfoResDTO>> queryList(@RequestBody AccountMemberQueryReqDTO reqDTO){

        return R.ok(accountMemberInfoService.queryList(reqDTO));
    }

    @DeleteMapping ("delete")
    public R<Boolean> delete(@Param("id") String id){
        return R.ok(accountMemberInfoService.removeById(id));
    }

}
